package com.example.accountprovider.dubbo;

import com.example.accountprovider.service.IAccountService;
import io.seata.core.context.RootContext;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboService;
import org.example.api.AccountApi;
import org.example.dto.AccountDto;
import org.example.exception.BizException;
import org.example.response.R;
import org.springframework.beans.factory.annotation.Autowired;

/**
 * @author Kong
 * @date 2021-9-13 15:38
 */
@Slf4j
@DubboService
public class AccountDobboService implements AccountApi {
    @Autowired
    private IAccountService accountService;

    @Override
    public R decrease(AccountDto dto) {
        log.info("[AT模式]全局事务XID:{}",RootContext.getXID());
//        try {
//            Thread.sleep(2000);
//        } catch (InterruptedException e) {
//            e.printStackTrace();
//        }
        int rs = accountService.decreaseAccount(dto.getUserId(),dto.getBalance().doubleValue());

        if (rs > 0) {
            return R.Builder.getInstance().buildOk().build();
        } else {
            throw new BizException("资金扣减异常：资金不足");
        }
    }
}
